package com.suixinsanren.design.state.demo.demo05;

import java.util.Random;

public class HasQuarterState implements State {

    private final GumballMachine gumballMachine;
    private final Random random;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
        this.random = new Random(System.currentTimeMillis());
    }

    @Override
    public void insertQuarter() {
        System.out.println("您已经投入硬币，请开始游戏");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("硬币已退还，欢迎下次光临");
        gumballMachine.setCurrentState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("游戏开始");
        if (gumballMachine.getCount() > 0) {
            if (random.nextInt(10) == 0) {
                System.out.println("您是大赢家，可以获得最多2颗糖果");
                gumballMachine.setCurrentState(gumballMachine.getWinnerState());
            } else {
                gumballMachine.setCurrentState(gumballMachine.getSoldState());
            }
        } else {
            gumballMachine.setCurrentState(gumballMachine.getSoldOutState());
        }
    }

    @Override
    public String toString() {
        return "已投入硬币";
    }
}
